using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Champ
{
    //Stores data for rendering and draws the texture to the screen
    public class Sprite
    {
        static private int numTextures = 20;
        public Texture2D[] texture = new Texture2D[numTextures];
        public Vector2[] position = new Vector2[numTextures];
        public Rectangle? sourceRect = null;
        public Color color = Color.White;
        public float rotation = 0f;
        public Vector2 origin = Vector2.Zero;
        public float scale = 1f;
        //Used to flip the texture along the horizontal or vertical axis
        public SpriteEffects effects = SpriteEffects.None;
        public float layerDepth = 0;
        public int whichTexture = 0; 
        public Boolean[] visible = new Boolean[numTextures];
        public Rectangle renderingRect;
        
        //Draws the texture 
        public void Draw(SpriteBatch spriteBatch)
        {
            if (position[whichTexture] == null)
                {
                    return;
                }
                if (texture == null)
                {
                    return;
                }
                spriteBatch.Draw(texture[0], renderingRect, sourceRect, color, rotation, origin, effects, layerDepth);
        }
        
        //If a sprite is made of multiple layers the layers[] will draw starting w/ the first layer listed in the array
        //At the moment avoid using.  Cheaply using for item glow at the moment.
        public void DrawLayers(SpriteBatch spriteBatch)
        {
           
            spriteBatch.Draw(texture[0], renderingRect, sourceRect, color, rotation, origin, effects, layerDepth);
            spriteBatch.Draw(texture[0], new Rectangle(renderingRect.X - 25, renderingRect.Y - 25, renderingRect.Width + 50, renderingRect.Height + 50), sourceRect, color, rotation, origin, effects, layerDepth);
            
        }
        
        //Takes in an angle (in degrees) and a position relative to the display vector and rotates it about that position.
        public void Rotate(double angle, Vector2 rotationAxis)
        {
            origin = rotationAxis;
            rotation +=(float)(angle * (Math.PI/180));
        }

        //Draws specified text centered at the point center
        public void DrawText(SpriteBatch spriteBatch, Vector2 center, String drawingText, SpriteFont gameFont, Color color)
        {
            Vector2 textSize = gameFont.MeasureString(drawingText);
            spriteBatch.DrawString(gameFont, drawingText,center - (textSize / 2), color); 
        }

        //Draws specified text centerd at the point center, and will adjust text to fit within bounds specified in maxLength
        //With font size not greater than largestSize+9
        public void DrawDynamicText(SpriteBatch spriteBatch, Vector2 center, String drawingText, float maxLength, SpriteFont[] gameFont, Color color, int largestSize)
        {
            int currentFont = largestSize;
            
            Vector2 textSize = gameFont[currentFont].MeasureString(drawingText);
            //Loops until the text is short enough or it reaches the smallest font
            while (textSize.Length() > maxLength && currentFont> 0)
            {
                currentFont--;
                textSize = gameFont[currentFont].MeasureString(drawingText);
            }
            //Draws the text at the font size that it determined in the while loop
            spriteBatch.DrawString(gameFont[currentFont], drawingText, center - (textSize / 2), color);

        }
    }
}